<?php

namespace zhtfrontend\controllers;

use common\models\datas\Balance;
use common\models\datas\Admissionfee;
use common\models\datas\BalanceRelation;
use doumiaoduo\buildform\BuildForm;
use Yii;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\Mechanic;
use yii\helpers\Json;
use yii\data\Pagination;
use yii\web\BadRequestHttpException;
use yii\web\Response;
use yii\helpers\ArrayHelper;
use common\components\extenders\AjaxObject;

class AdmissionaccountController extends \zhtfrontend\components\bases\BaseController
{

    public function actionIndex()
    {
        $search_data = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = Balance::find()->where(['type' => 0, 'site_id' => Fun::getSiteId()]);
        if (!empty($search_data)) {
            if (isset($search_data['mechanic_id']) && $search_data['mechanic_id'] > 0) {
                $data->andWhere(['mechanic_id' => $search_data['mechanic_id']]);
            }
            if (isset($search_data['month']) && $search_data['month'] > 0) {
                $month = $search_data['month'];
                $time = strtotime($month . '01');
                $y = substr($month, 0, 4);
                $m = substr($month, -2);
                $sum_day = date("t", mktime(0, 0, 0, $m, 1, $y));
                $data->andWhere(['between', 'created_at', $time, strtotime($month . $sum_day . ' 23:59:59')]);
            }
            if (isset($search_data['state']) && $search_data['state'] != null) {
                $data->andWhere(['state' => $search_data['state']]);
            }
        }

        $mids = Fun::getMechanicIds();
        if (!empty($mids)) {
            $data->andWhere(['mechanic_id' => $mids]);
        }
        $data->orderBy('balance_id DESC');
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['mechanic_name', 'status', 'month', 'operate_admission']);

        //获取月份
        $months_list = Fun::getSysDb()->createCommand('SELECT FROM_UNIXTIME(created_at,"%Y%m") as months FROM {{%balance}} WHERE type = 0 GROUP BY months')->queryAll();
        $months = ArrayHelper::map($months_list, 'months', 'months');
        //获取机构
        $modelMechanic = Mechanic::find();
        if (!empty($mids)) {
            $modelMechanic->andWhere(['id' => $mids]);
        }
        $mechanic = $modelMechanic->all();
        $listData = ArrayHelper::map($mechanic, 'id', 'name');
        return $this->render('index', ['listData' => $listData, 'months' => $months, 'data' => $array, 'pages' => $pages]);
    }

    /**
     * Displays a single Settlement model.
     */
    public function actionView()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $admissionfee_id = BalanceRelation::find()->select('relation_id')->where(['balance_id' => \Yii::$app->request->get('id'), 'type' => 0])->asArray()->column();
        $model = Admissionfee::find()->where(['id' => $admissionfee_id])->orderBy('id DESC')->all();
        $admission_list = Fun::asArray($model, ['goods_name', 'student_name', 'admissionfee', 'created_at']);
        return $this->render('view', ['model' => $admission_list]);
    }


    //结算
    public function actionState()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $model = Balance::findOne(['balance_id' => \Yii::$app->request->get('id'), 'type' => 0]);
        if ($model == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $form = new BuildForm('@app/components/forms/balanceForm.php');
        $form->loadModel(['balance' => $model]);
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model->updated_at = time();
            $model->state = 1;
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '结算成功'), Fun::url(['admissionaccount/index']), true);
            }
        }
        return $this->render('settlement', ['form' => $form->render(), 'model' => $model]);
    }

    public function actionDelete()
    {
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $model = Balance::findOne(\Yii::$app->request->get('id'));
        if ($model == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        if ($model->delete()) {
            $adminssionids = BalanceRelation::find()->select('relation_id')->where(['balance_id' => \Yii::$app->request->get('id'), 'type' => 0])->column();
            Admissionfee::updateAll(['status' => 0], ['id' => $adminssionids]);
            BalanceRelation::deleteAll('balance_id = :balance_id AND type=:type', [':balance_id' => \Yii::$app->request->get('id'), ':type' => 0]);
            return $this->redirectMessage(\Yii::t('zhtfrontend', '删除成功'), Fun::url(['admissionaccount/index']), true);
        }

    }
}
